package com.zat.mes.andrmes.dofun.qualitytest

import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.data.KvBean
import com.zat.mes.andrmes.data.QcRecordBean
import com.zat.mes.andrmes.data.QcRecordHasBean
import com.zat.mes.andrmes.dofun.qualitytest.adapter.OnSiteDetailAdapter
import com.zat.mes.andrmes.dofun.qualitytest.vm.QcPlanViewModel
import com.zat.mes.andrmes.event.TodoEventMessage
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.global.CompanionType.Companion.INTENT_EXTRA_QCID
import com.zat.mes.andrmes.global.CompanionType.Companion.INTENT_EXTRA_TODO
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.activity_on_site_has.*
import kotlinx.android.synthetic.main.layout_title.*
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

/**
 * 巡检详情 已完成
 */
class OnSiteHasActivity : BaseActivity() {

    private var todo: Boolean = true;
    private var onSiteDetailAdapter: OnSiteDetailAdapter? = null
    private var onSiteResultAdapter: OnSiteDetailAdapter? = null
    private var qcPlanViewModel: QcPlanViewModel? = null
    private var taskId: String? = null
    var bean: QcRecordBean? = null
    var qcId: Long = 0
    /**
     * 1合格 0不合格
     */
    var result: String = "1"

    override fun init() {
        initView()
        iniData()
        loadModel()
    }

    /**
     * 初始化datamodel
     */
    private fun loadModel() {
        qcPlanViewModel = ViewModelProvider(this).get(QcPlanViewModel::class.java)
        qcPlanViewModel?.getQcDetailData()?.observe(this, Observer {
            closeLoading()
            if (it != null) {
                setNewData(it)
            }


        })
        qcPlanViewModel?.selectQcRecordById(qcId)
        showLoading()
        qcPlanViewModel?.getQcWorkSuData()?.observe(this, Observer {
            closeLoading()
            if (getString(R.string.post_success).equals(it)) {
                EventBus.getDefault().post(TodoEventMessage())
            }
            showToast(it)
            finish()
        })
    }

    /**
     * 设置数据
     */
    private fun setNewData(bean: QcRecordHasBean) {
        val list: MutableList<KvBean> = ArrayList()
        val kv1 = bean?.productName?.let {
            KvBean(
                getString(R.string.produce_name),
                it
            )
        }
        kv1?.let { list.add(it) }
        val kv2 = bean?.styleNo?.let {
            KvBean(
                getString(R.string.text_style),
                it
            )
        }
        kv2?.let { list.add(it) }
        val kv3 = bean?.color?.let { KvBean(getString(R.string.text_color), it) }
        kv3?.let { list.add(it) }
        val kv4 = bean?.size?.let { KvBean(getString(R.string.text_size), it) }
        kv4?.let { list.add(it) }
        val kv5 = bean?.procName?.let {
            KvBean(
                getString(R.string.text_proc),
                it
            )
        }
        kv5?.let { list.add(it) }
        val kv6 = bean?.workshopName?.let {
            KvBean(
                getString(R.string.text_workshop),
                it
            )
        }
        kv6?.let { list.add(it) }
        val kv7 = bean?.className?.let {
            KvBean(
                getString(R.string.text_class),
                it
            )
        }
        kv7?.let { list.add(it) }
        val kv8 = bean?.workEmpName?.let {
            KvBean(
                getString(R.string.text_peple),
                it
            )
        }
        kv8?.let { list.add(it) }
        val kv9 =
            KvBean(getString(R.string.text_amount), bean?.quantity.toString())
        list.add(kv9)
        val kv10 =
            KvBean(getString(R.string.check_amount), bean?.qcQty.toString())
        list.add(kv10)
        val kv11 =
            KvBean(getString(R.string.bad_produce_amount), bean?.ngqty.toString())
        list.add(kv11)
        if (bean?.bodypartName != null) {
            val kv12 =
                KvBean(getString(R.string.body_path), bean?.bodypartName?.toString())
            list.add(kv12)
        }
        if ("1" == bean?.result) {
            val kv13 = KvBean(getString(R.string.qc_result), getString(R.string.hege))
            list.add(kv13)
        } else {
            val kv13 = KvBean(getString(R.string.qc_result), getString(R.string.hege_no))
            list.add(kv13)
        }
        var kv14 = KvBean(getString(R.string.buchongshuoming), "")
        if (!TextUtils.isEmpty(bean?.remark1)) {
            kv14.value = bean?.remark1
        }
        list.add(kv14)

        var kv15 = KvBean(getString(R.string.bad_result),"")
        if(!TextUtils.isEmpty(bean?.ngreason)){
            kv15.value = bean?.ngreason
            list.add(kv15)
        }
        var kv16 = KvBean(getString(R.string.deal_method_2),"")
        if(!TextUtils.isEmpty(bean?.ngdeal)){
            kv16.value = bean?.ngdeal
            list.add(kv16)
        }

        onSiteDetailAdapter?.setNewData(list)

        val resultList: MutableList<KvBean> = ArrayList()
        val kva = bean?.qcEmpName?.let {
            KvBean(
                getString(R.string.text_on_site_people),
                it
            )
        }
        kva?.let { resultList.add(it) }
        val kvb = bean?.reviewEmpName?.let {
            KvBean(
                getString(R.string.text_class_leader),
                it
            )
        }
        kvb?.let { resultList.add(it) }
        val kvc = bean?.treatment?.let {
            KvBean(
                getString(R.string.deal_method),
                it
            )
        }
        kvc?.let { resultList.add(it) }
        val kvd = bean?.fineAmt?.let {
            KvBean(
                getString(R.string.withhold_money),
                it.toString()
            )
        }
        kvd?.let { resultList.add(it) }
        val kve = bean?.repairEmpName?.let {
            KvBean(
                getString(R.string.repair_people),
                it
            )
        }
        kve?.let { resultList.add(it) }
        val kvf = bean?.disuseQty?.let {
            KvBean(
                getString(R.string.delete_amount),
                it.toString()
            )
        }
        kvf?.let { resultList.add(it) }
        val kvg = bean?.repairQty?.let {
            KvBean(
                getString(R.string.repair_amount),
                it.toString()
            )
        }
        kvg?.let { resultList.add(it) }
        val kvh = bean?.remark2?.let {
            KvBean(
                getString(R.string.remark),
                it
            )
        }
        kvh?.let { resultList.add(it) }
        onSiteResultAdapter?.setNewData(resultList)
    }

    /**
     * 初始化数据
     */
    private fun iniData() {

        qcId = intent.getLongExtra(INTENT_EXTRA_QCID, 0)
        todo = intent.getBooleanExtra(INTENT_EXTRA_TODO, false);
        taskId = intent.getStringExtra(CompanionType.INTENT_EXTRA_TASKID)
        onSiteDetailAdapter = OnSiteDetailAdapter(R.layout.item_de_approve)
        ryOnSiteDetailHas.layoutManager = LinearLayoutManager(this)
        ryOnSiteDetailHas.adapter = onSiteDetailAdapter

        onSiteResultAdapter = OnSiteDetailAdapter(R.layout.item_de_approve)
        ryOnSiteHas.layoutManager = LinearLayoutManager(this)
        ryOnSiteHas.adapter = onSiteResultAdapter
        //是否待办
        if (todo) {
            tvOkOnSiteHas.visibility = View.VISIBLE
        } else {
            tvOkOnSiteHas.visibility = View.GONE;
        }

        //质检已分派 后 完成任务 点击完成
        tvOkOnSiteHas.setOnClickListener {
            //调用接口
            val jsonObject = JSONObject()
            jsonObject.put("qcId", qcId)
            jsonObject.put("taskId", taskId)
            qcPlanViewModel?.workQcRecordSu(jsonObject.toString());
            showLoading()
        }

    }


    private fun initView() {
        setContentView(R.layout.activity_on_site_has)
        tVtitle.text = getString(R.string.title_on_site_has)
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }

    }


}
